
package com.sc.datacollect.auth;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.sc.datacollect.util.Common;

/**
 * 认证配置
 * 
 * @author dong
 *
 */
public class AuthConfig {

	private static List<Map<String, Object>> authentication = new ArrayList<Map<String, Object>>(5);

	private AuthConfig() {
	}

	private static AuthConfig authConfig;

	/**
	 * 获取认证实例
	 * 
	 * @return
	 */
	public static AuthConfig getInstance() {
		if (null == authConfig) {
			authConfig = new AuthConfig();
			initConfig();
		}
		return authConfig;

	}

	/**
	 * 获取认证集合
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getAuthentications() {
		return authentication;
	}

	/**
	 * 初始化认证信息
	 */
	private static void initConfig() {
		String src = Common.dir + "auth.properties";
		FileInputStream is = null;
		try {
			is = new FileInputStream(src);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		Properties properties = new Properties();
		try {
			properties.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Iterator<Object> its = properties.keySet().iterator();
		while (its.hasNext()) {
			String key = (String) its.next();
			if (key.startsWith("UserPwd", 3)) {
				String value = properties.getProperty(key);
				String[] userPwd = value.split(",");
				Map<String, Object> map = new HashMap<String, Object>(2);
				map.put("appId", userPwd[0]);
				map.put("appKey", userPwd[1]);
				authentication.add(map);
			}
		}
	}
}
